package com.itheima.test01;

import java.io.*;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class Test {
    public static void main(String[] args) throws IOException {
        TreeSet<Student> treeStu = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = -(o1.Total()-o2.Total());
                // result = result==0? -(Integer.parseInt(o1.getChinese())-Integer.parseInt(o2.getChinese())) : result;
                //result = result==0? -(Integer.parseInt(o1.getMath())-Integer.parseInt(o2.getMath())) : result;
                result = result==0? -(Integer.parseInt(o1.getEnglish())-Integer.parseInt(o2.getEnglish())) : result;
                return result;
            }
        });

        //输入并封装之后添加到集合
        Scanner sc = new Scanner(System.in);
        for (int i = 1; i <= 3; i++) {
            System.out.println("姓名:");
            String name = sc.next();
            System.out.println("语文成绩：");
            String chinese = sc.next();
            System.out.println("数学成绩：");
            String math = sc.next();
            System.out.println("英语成绩：");
            String english = sc.next();
            treeStu.add(new Student(name,chinese,math,english));
            if (i<3) {
                System.out.println("请录入第" + (i + 1) + "个学生信息：");
            }
        }
        //System.out.println(treeStu);

        //写入文本
        BufferedWriter bw = new BufferedWriter(new FileWriter("exam2\\test01.txt",true));
        bw.write("姓名，语文成绩，数学成绩，英语成绩");
        bw.newLine();
        bw.flush();
        for (Student stu : treeStu) {
            String name = stu.getName();
            bw.write(name+",");
            String chinese = stu.getChinese();
            bw.write(chinese+",");
            String math = stu.getMath();
            bw.write(math+",");
            String english = stu.getEnglish();
            bw.write(english+",");
            bw.newLine();
            bw.flush();
        }
        bw.close();
        System.out.println("数据写入文件完毕");

        //读取数据：
        System.out.println("文件中的数据展示如下：");
        BufferedReader br = new BufferedReader(new FileReader("exam2\\test01.txt"));
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();



    }
}
